package com.fht.controller.food;


import com.fht.entity.FoodCategory;
import com.fht.entity.FoodRecord;
import com.fht.entity.MerchantsInformation;
import com.fht.enums.ManagermentSystemEnum;
import com.fht.from.FoodCategoryFrom;
import com.fht.service.food.FoodCategoryService;
import com.fht.service.food.FoodRecordService;
import com.fht.service.food.MerchantsInformationService;
import com.fht.util.ResultVOUtil;
import com.fht.vo.LayUITableVO;
import com.fht.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/food/record")
public class foodRecordController {


    @Autowired
    public FoodRecordService foodRecordService;

    @Autowired
    public FoodCategoryService foodcategoryService;

    @Autowired
    public MerchantsInformationService merchantsInformationService;



    //菜品类别展示
    @GetMapping("/index")
    public String index(Model model){

        List<MerchantsInformation> merchantsInformationList = merchantsInformationService.findAllIdAndName();
        model.addAttribute("merchantsInformationList",merchantsInformationList);

        return "food/foodRecord/index";

    }

    //获取菜品类别信息
    @GetMapping("/list")
    @ResponseBody
    public LayUITableVO<List<FoodRecord>> list (@RequestParam(defaultValue = "1") Integer page,
                                                  @RequestParam(defaultValue = "10") Integer limit,
                                                  FoodCategoryFrom from){
        Map<String ,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("from",from);

        LayUITableVO<List<FoodRecord>> foodRecordList = foodRecordService.selectByFrom(map);


        return foodRecordList;
    }


    //修改菜品类别信息1（查询指定id菜品分类详情）
    @GetMapping("/edit-ui")
    public String editUI(String id,Model model){

        FoodRecord foodRecord = foodRecordService.selectById(id);

        Map<String,Object> map  = foodcategoryService.findParentsAndChildsById(foodRecord.getCategoryId());

        foodRecord.setCategoryParentId(Integer.valueOf( (String) map.get("categoryParentId")));
        model.addAttribute("foodRecord",foodRecord);
        model.addAttribute("map",map);

        return "food/foodRecord/edit";

    }

    //查询二级分类目录
    @GetMapping("/typeLevel")
    @ResponseBody
    public ResultVO levelTwo(String parentId){
        if (null == parentId||"".equals(parentId)){
            log.info("【查询分级目录】 接收参数有误！ parentId={}",parentId);
            return ResultVOUtil.error(ManagermentSystemEnum.PARAM_ERROR);
        }
        //查询全部二级目录
        List<FoodCategory> foodCategoryList = foodcategoryService.findByParentId(parentId);

        return ResultVOUtil.ok(foodCategoryList);
    }

    //执行修改
    @PostMapping("/edit")
    @ResponseBody
    public ResultVO edit(FoodRecord foodRecord){

        foodRecordService.update(foodRecord);

        return ResultVOUtil.ok();
    }

    //执行删除
    @PostMapping("/del")
    @ResponseBody
    public ResultVO del(String id){
        foodRecordService.delelteById(id);
        return ResultVOUtil.ok();
    }

    //跳转至添加页面
    @GetMapping("/add-ui")
    public String addUI(String merchantsId,Model model){

        List<FoodCategory> foodCategoryList = foodcategoryService.selectBymerchantsId(merchantsId);
        model.addAttribute("foodCategoryList",foodCategoryList);
        model.addAttribute("merchantsId",merchantsId);
        return "food/foodRecord/add";
    }

    //执行修改
    @PostMapping("/add")
    @ResponseBody
    public ResultVO add(FoodRecord foodRecord){

        foodRecordService.add(foodRecord);

        return ResultVOUtil.ok();
    }


    //修改菜品类别信息1（查询指定id菜品分类详情）
    @GetMapping("/editRepertory-ui")
    public String editRepertoryUI(String id,Model model){

        FoodRecord foodRecord = foodRecordService.selectById(id);


        model.addAttribute("foodRecord",foodRecord);


        return "food/foodRecord/editRepertory";

    }

    //执行修改
    @PostMapping("/editRepertory")
    @ResponseBody
    public ResultVO editRepertory(FoodRecord foodRecord){

        foodRecordService.updateRepertory(foodRecord);



        return ResultVOUtil.ok();
    }
}
